<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layuimini-2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuimini-2/css/layuimini.css" media="all">


</head>
<body>
<!--      模糊搜索,数据绑定-->
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">租金</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="rent" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">楼层</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="floor" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">面积</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="area" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">主题</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="subject" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">

                                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                <button type="submit" class="layui-btn layui-btn-primary"  ><i class="layui-icon"></i> 全部搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>


            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deleteBatch"> 批量删除 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="img">图片详情</a>
            </script>

        </div>
    </div>
    <script src="../../../layuimini-2/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        var $ ;
        var form;
        var table;
        var layer;
        layui.use(['form','table',"jquery"], function () {
             $ = layui.jquery;
             form = layui.form;
             table = layui.table;

            //渲染表格
            tableRender();

            // 监听模糊搜索操作
            form.on('submit(data-search-btn)', function (obj) {
                table.render({
                    elem: '#currentTableId',
                    url: '/feast/list.do',
                    toolbar: '#toolbarDemo',
                    where:obj.field,
                    page: true,
                    limit: 6,
                    limits: [6, 12, 18, 24],
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        {type: "checkbox", width: 50},
                        {field: 'id', width: 80, title: 'ID', sort: true},
                        {field: 'floor', width: 80, title: '楼层'},
                        {field: 'room', width: 80, title: '房间号', sort: true},
                        {field: 'area', width: 80, title: '面积'},
                        {field: 'num', title: '人数', width: 70},
                        {field: 'preTime', width: 120, title: '预定时间', sort: true, templet: function (obj) {
                                let date = new Date(obj.preTime);
                                let year = date.getFullYear();
                                let month = date.getMonth() + 1;
                                if (month < 10) {
                                    month = "0" + month
                                }
                                let day = date.getDate();
                                if (day < 10) {
                                    day = "0" + day
                                }
                                return year + "-" + month + "-" + day
                            }},
                        {field: 'endTime', width: 120, title: '结束时间', sort: true, templet: function (obj) {
                                let date = new Date(obj.endTime);
                                let year = date.getFullYear();
                                let month = date.getMonth() + 1;
                                if (month < 10) {
                                    month = "0" + month
                                }
                                let day = date.getDate();
                                if (day < 10) {
                                    day = "0" + day
                                }
                                return year + "-" + month + "-" + day
                            }},
                        {field: 'rent', width: 80, title: '租金'},
                        {field: 'feastName', width: 135, title: '预定人', sort: true},
                        {field: 'subject', width: 135, title: '主题', sort: true},

                        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                    ]],
                    skin: 'line'
                });
                return false;
            });

            /**
             * toolbar监听添加,批量删除事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    layer.open({
                        title: '添加用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/view/hall/feast/feastAdd.html',
                        end: function () {
                            table.reload("currentTableId")
                        }
                    });
                    $(window).on("resize", function () {
                        // layer.full(index);
                    });
                } else if (obj.event === 'deleteBatch') {  // 监听批量删除操作
                    var checkStatus = table.checkStatus("currentTableId");
                    if (checkStatus.data.length !=0) {
                        var arr = [];
                        for (var i=0;i < checkStatus.data.length;i++) {
                            arr.push(checkStatus.data[i].id);
                        }
                        $.ajax({
                            url: "/feast/batch.do",
                            data:{ids:arr},
                            type:"get",
                            traditional:true,
                            success: function (ret) {
                                if (ret.code==0) {
                                    layer.msg("删除成功!")
                                    table.reload("currentTableId");
                                }
                            },
                            error:function () {
                                layer.msg("服务器忙!");
                            }
                        })
                    } else {
                        layer.alert("请选择需要删除的数据",{icon:0,title:'提示'} )
                    }
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                // console.log(obj)
            });

            /**
             * 编辑以及删除信息
             */
            table.on('tool(currentTableFilter)', function (obj) {
                if (obj.event === 'edit') {
                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/view/hall/feast/feastEdit.html',
                        success: function (layero, index) {//弹出层open方法
                            let body = layer.getChildFrame("body", index);
                            let feastData = obj.data;
                            console.log(feastData)
                            body.find("input[name=id]").val(feastData.id)
                            body.find("input[name=floor]").val(feastData.floor)
                            body.find("input[name=room]").val(feastData.room)
                            body.find("input[name=area]").val(feastData.area)
                            body.find("input[name=num]").val(feastData.num)
                            let date = new Date(feastData.preTime);
                            let year = date.getFullYear();
                            let month = date.getMonth() + 1;
                            if (month < 10) {
                                month = "0" + month;
                            }
                            let day = date.getDate();
                            if (day < 10) {
                                day = "0" + day;
                            }
                            let addTime1 = year + "-" + month + "-" + day;
                            body.find("input[name=preTime]").val(addTime1)

                            let date1 = new Date(feastData.endTime);
                            let year1 = date1.getFullYear();
                            let month1 = date1.getMonth() + 1;
                            if (month1 < 10) {
                                month1 = "0" + month1;
                            }
                            let day1 = date.getDate();
                            if (day1 < 10) {
                                day1 = "0" + day1;
                            }
                            let updateTime3 = year1 + "-" + month1 + "-" + day1;
                            body.find("input[name=endTime]").val(updateTime3)

                            body.find("input[name=rent]").val(feastData.rent)
                            body.find("input[name=feastName]").val(feastData.feastName)
                            body.find("input[name=subject]").val(feastData.subject)
                            body.find('#image').attr("src",feastData.img);
                        },
                        end: function () {
                            table.reload("currentTableId")
                        }
                    });
                    $(window).on("resize", function () {
                        // layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm("确认删除", {icon: 3, title: "提示"}, function (index) {
                        //layer.msg("删除成功")
                        //发送请求
                        $.get("/feast/delete.do", {id: obj.data.id}, function (ret) {
                            if (ret.code == 0) {
                                layer.msg("删除成功")
                            } else {
                                layer.msg("删除失败")
                            }
                        });
                        layer.close(index);
                        table.reload("currentTableId")
                    }, function (index) {
                        layer.msg("取消删除")
                        layer.close(index)
                    });
                }else if (obj.event === 'img') {
                    layer.open({
                        type:2,
                        content:"img.html",
                        area:["400px","600px"],
                        title:"图片详情页面",
                        success:function (layero,index) {
                            let body = layer.getChildFrame("body",index);
                            let Data = obj.data;
                            body.find("#image").attr("src",Data.img)
                        },
                        end:function () {
                            table.reload("currentTableId")
                        }
                    })
                }
            });
        });

        /**
         * 渲染表格
         * @param obj 数据
         */
        function tableRender(obj) {
            table.render({
                elem: '#currentTableId',
                url: '/feast/list.do',
                toolbar: '#toolbarDemo',
                page: true,
                limit: 6,
                limits: [6, 12, 18, 24],
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'floor', width: 80, title: '楼层'},
                    {field: 'room', width: 100, title: '房间号', sort: true},
                    {field: 'area', width: 80, title: '面积'},
                    {field: 'num', title: '人数', width: 70},
                    {field: 'preTime', width: 120, title: '预定时间', sort: true, templet: function (obj) {
                            let date = new Date(obj.preTime);
                            let year = date.getFullYear();
                            let month = date.getMonth() + 1;
                            if (month < 10) {
                                month = "0" + month
                            }
                            let day = date.getDate();
                            if (day < 10) {
                                day = "0" + day
                            }
                            return year + "-" + month + "-" + day
                        }},
                    {field: 'endTime', width: 120, title: '结束时间', sort: true, templet: function (obj) {
                            let date = new Date(obj.endTime);
                            let year = date.getFullYear();
                            let month = date.getMonth() + 1;
                            if (month < 10) {
                                month = "0" + month
                            }
                            let day = date.getDate();
                            if (day < 10) {
                                day = "0" + day
                            }
                            return year + "-" + month + "-" + day
                        }},
                    {field: 'rent', width: 80, title: '租金'},
                    {field: 'feastName', width: 135, title: '预定人', sort: true},
                    {field: 'subject', width: 135, title: '主题', sort: true},
                    {field: 'img', width: 135, title: '图片路径', sort: true},
                    // {field: 'img',  title: '图片路径',align:'center', templet:"<div><img  src=\"{{ d.image }}\" ></div>"},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                skin: 'line'
            });
        }


    </script>

</body>
</html>